package org.lanqiao.userprofile.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.lanqiao.userprofile.movie.entity.DwdUserInfo;
import org.lanqiao.userprofile.movie.service.DwdUserInfoService;
import org.lanqiao.userprofile.movie.service.impl.DwdUserInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author root
 * @since 2023-09-26
 */
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private DwdUserInfoServiceImpl dwdUserInfoService;

    @RequestMapping("/")
    public String index(HttpServletRequest request) {
        return "login.html";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(name + ":" + password);

        if (name.equals("admin") && password.equals("admin")) {
            // 管理员账户
            return "redirect:/ods-movie-info/admin";
        } else {
            LambdaQueryWrapper<DwdUserInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DwdUserInfo::getUserToken, name);
            DwdUserInfo dwdUserInfo = dwdUserInfoService.getOne(queryWrapper);
            if (dwdUserInfo != null) {
                HttpSession sessoin = request.getSession();
                sessoin.setAttribute("name", name);
                return "redirect:/ods-movie-info/index";
            } else {
                request.setAttribute("msg","用户名不存在");
                return "login.html";
            }
        }

    }


}
